try:
  from coin.fastfeature.feature_product_pb2 import FeatureProductProto
except Exception:
  from fastfeature.feature_product_pb2 import FeatureProductProto

from coin.exchange.okex_futures.kr_rest.futures_product import (OkexFuturesProduct)
from coin.exchange.huobi_futures.kr_rest.futures_product import (HuobiFuturesProduct)
from coin.exchange.kr_rest.product.product_impl import generate_product_from_str
from coin.exchange.kraken_futures.kr_rest.futures_product import KrakenFuturesProduct
from coin.exchange.binance_futures.kr_rest.futures_product import (BinanceFuturesProduct)


def get_prefix(product):
  return "_".join((product.exchange, product.symbol))


def product_proto_from_symbol_dict(symbol):
  return FeatureProductProto(
      exchange=symbol['exchange'],
      product_name=symbol['product_name'],
      symbol_string=symbol['symbol_string'],
  )


def product_from_symbol_dict(symbol, current_datetime):
  if symbol['exchange'] == 'OkexFutures':
    return OkexFuturesProduct.FromStr(symbol['product_name'], current_datetime)
  elif symbol['exchange'] == 'HuobiFutures':
    return HuobiFuturesProduct.FromStr(symbol['product_name'], current_datetime)
  elif symbol['exchange'] == 'KrakenFutures':
    return KrakenFuturesProduct.FromStr(symbol['product_name'], current_datetime)
  elif symbol['exchange'] == 'BinanceFutures':
    return BinanceFuturesProduct.FromStr(symbol['product_name'], current_datetime)
  else:
    return generate_product_from_str(symbol['exchange'].replace("Futures", ""),
                                     None,
                                     symbol['product_name'])


def product_from_symbol_dicts(symbols, current_datetime):
  return [product_from_symbol_dict(symbol, current_datetime) for symbol in symbols]


def product_map_from_symbol_dicts(symbols, current_datetime):
  return {
      product_from_symbol_dict(symbol, current_datetime): product_proto_from_symbol_dict(symbol)
      for symbol in symbols
  }


def aggregation_map_from_symbol_dicts(symbols, current_datetime):
  return {
      product_from_symbol_dict(symbol, current_datetime): symbol.get('aggregation_size', None)
      for symbol in symbols
  }


def product_map_from_products(products):
  return {
      product: FeatureProductProto(exchange=product.exchange,
                                   product_name=product.symbol,
                                   symbol_string=product.exchange + "-" + product.symbol)
      for product in products
  }
